﻿// --------------------------------------------------------------------------------------------------------------------
// <copyright company="DHGMS Solutions" file="BuildMissingIndexes.cs">
//   2004-2012 DHGMS Solutions. Some Rights Reserved. Licensed under GNU General Public License version 2 (GPLv2)
// </copyright>
// --------------------------------------------------------------------------------------------------------------------

namespace Dhgms.TplHelper.Controller.Task.Sql
{
    using System.Data.SqlClient;
    using System.Reflection;

    using Dhgms.DataManager.Model.Helper.Database;

    /// <summary>
    /// Task for building indexes reported as missing by MS SQL Server
    /// </summary>
    public class BuildMissingIndexes : EmbeddedInteger32<MSSql10, SqlParameter>
    {
        /// <summary>
        /// Flag to indiciate whether the index should use included columns
        /// </summary>
        private readonly bool useIncludedColumns;

        /// <summary>
        /// The name of the index file group
        /// </summary>
        private readonly string indexFileGroupName;

        #region Constructors and Destructors

        /// <summary>
        /// Initializes a new instance of the <see cref="BuildMissingIndexes"/> class. 
        /// </summary>
        /// <param name="connectionString">
        /// Database connection string
        /// </param>
        /// <param name="indexFileGroupName">
        /// File Group the indexes are to be stored on
        /// </param>
        /// <param name="useIncludedColumns">
        /// Whether to build indexes with "included" columns
        /// </param>
        public BuildMissingIndexes(string connectionString, string indexFileGroupName, bool useIncludedColumns)
            : base(
                "Building Missing Indexes", 
                connectionString, 
                Assembly.GetExecutingAssembly(), 
                "Dhgms.TplHelper.Resource.Sql", 
                "BuildMissingIndexes.sql", 
                true)
        {
            this.indexFileGroupName = indexFileGroupName;
            this.useIncludedColumns = useIncludedColumns;
        }

        #endregion

        /// <summary>
        /// Gets a list of parameters for the SQL command
        /// </summary>
        /// <returns>
        /// list of parameters for the SQL command
        /// </returns>
        protected override SqlParameter[] GetParameters()
        {
            return new[]
                {
                    new SqlParameter("@useIncludeColumns", this.useIncludedColumns),
                    new SqlParameter("@indexFileGroupName", this.indexFileGroupName)
                };
        }
    }
}